
local fonts =
{
   Calibri        = 'Calibri1.ttf',
   CalibriB       = 'CalibriBold.ttf',
   CalibriI       = 'CalibriItalic.ttf',
   CalibriBI      = 'CalibriBoldItalic.ttf',
   
   Consolas       = 'CONSOLA.ttf',
   ConsolasB      = 'CONSOLAB.ttf',
   ConsolasI      = 'CONSOLAI.ttf',
   ConsolasBI     = 'CONSOLAZ.ttf',
   
   ABF            = 'ABF.ttf',
   TwCenMT        = 'Tw_Cen_MT_Bold.ttf'
}

local media = Spy.media
local fontLoc = [[Interface\AddOns\SpyHud\Fonts\]]

for k, v in pairs(fonts) do
   fonts[k] = fontLoc..v
   media:Register('font', k, fonts[k])
end

if not Spy.fonts then Spy.fonts = {} end

function Spy.CreateFont(name, font, size)
   local f = CreateFont('SpyFont_'..name)
   f:SetFont(font, size)
   Spy.fonts[name] = f
   return f
end

Spy.CreateFont('code', Font('Consolas'), 12)
Spy.CreateFont('chat', 'Fonts\\ARIALN.ttf', 12)

function SetFontSize(frame, size)
   local font, oldSize = frame:GetFont()
   frame:SetFont(font, size)
end

local function CanSetFont(object)
	return (type(object)=='table'
		and object.SetFont and object.IsObjectType 
			and not object:IsObjectType('SimpleHTML')); 
end

function Spy.ApplyFonts()

	local calibri     = fonts.Calibri
	local abf         = fonts.ABF
	local twcenmt     = media:Fetch('font', 'TwCenMT') or calibri
   local arialn      = 'Fonts\\ARIALN.TTF'
   
   local defFont     = calibri
   local chatFont    = arialn
   local sctFont     = twcenmt
	local statusFont  = defFont
   
   local NORMAL_TEXT_FONT     = fonts.Calibri
   local BOLD_TEXT_FONT       = fonts.CalibriB
   local BOLDITALIC_TEXT_FONT = fonts.CalibriBI
   local ITALIC_TEXT_FONT     = fonts.CalibriI
   local NUMBER_TEXT_FONT     = fonts.CalibriB

   CHAT_FONT_HEIGHTS = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
   
   UNIT_NAME_FONT = NORMAL_TEXT_FONT
   NAMEPLATE_FONT = NORMAL_TEXT_FONT
   DAMAGE_TEXT_FONT = sctFont
   STANDARD_TEXT_FONT = NORMAL_TEXT_FONT

   UIDROPDOWNMENU_DEFAULT_TEXT_HEIGHT = 12

   SystemFont_Tiny:SetFont(NORMAL_TEXT_FONT, 9)
   SystemFont_Small:SetFont(NORMAL_TEXT_FONT, 11)
   SystemFont_Outline_Small:SetFont(NORMAL_TEXT_FONT, 11)
   SystemFont_Shadow_Small:SetFont(NORMAL_TEXT_FONT, 11)
   SystemFont_InverseShadow_Small:SetFont(NORMAL_TEXT_FONT, 11)
   SystemFont_Med1:SetFont(NORMAL_TEXT_FONT, 13)
   SystemFont_Shadow_Med1:SetFont(NORMAL_TEXT_FONT, 13)
   SystemFont_Med2:SetFont(NORMAL_TEXT_FONT, 14)
   SystemFont_Med3:SetFont(BOLD_TEXT_FONT, 15)
   SystemFont_Shadow_Med3:SetFont(BOLD_TEXT_FONT, 15)
   SystemFont_Large:SetFont(BOLD_TEXT_FONT, 17)
   SystemFont_Shadow_Large:SetFont(BOLD_TEXT_FONT, 17)
   SystemFont_Shadow_Huge1:SetFont(BOLD_TEXT_FONT, 20)
   SystemFont_OutlineThick_Huge2:SetFont(BOLD_TEXT_FONT, 22)
   SystemFont_Shadow_Outline_Huge2:SetFont(BOLD_TEXT_FONT, 22)
   SystemFont_Shadow_Huge3:SetFont(BOLD_TEXT_FONT, 25)
   SystemFont_OutlineThick_Huge4:SetFont(BOLD_TEXT_FONT, 26)
   SystemFont_OutlineThick_WTF:SetFont(BOLD_TEXT_FONT, 102)

   DialogButtonNormalText:SetFont(NORMAL_TEXT_FONT, 17)

   GameFontNormal:SetFont(NORMAL_TEXT_FONT, 12)
   GameFontHighlight:SetFont(NORMAL_TEXT_FONT, 12)
   GameFontDisable:SetFont(NORMAL_TEXT_FONT, 12)
   GameFontDisable:SetTextColor(0.6, 0.6, 0.6)
   GameFontGreen:SetFont(NORMAL_TEXT_FONT, 12)
   GameFontRed:SetFont(NORMAL_TEXT_FONT, 12)
   GameFontBlack:SetFont(NORMAL_TEXT_FONT, 12)
   GameFontWhite:SetFont(NORMAL_TEXT_FONT, 12)

   GameFontNormalSmall:SetFont(NUMBER_TEXT_FONT, 11)
   GameFontHighlightSmall:SetFont(NUMBER_TEXT_FONT, 11)
   GameFontHighlightSmallOutline:SetFont(NUMBER_TEXT_FONT, 11, 'OUTLINE')
   GameFontDisableSmall:SetFont(NUMBER_TEXT_FONT, 11)
   GameFontDisableSmall:SetTextColor(0.6, 0.6, 0.6)
   GameFontDarkGraySmall:SetFont(NUMBER_TEXT_FONT, 11)
   GameFontDarkGraySmall:SetTextColor(0.4, 0.4, 0.4)
   GameFontGreenSmall:SetFont(NUMBER_TEXT_FONT, 11)
   GameFontRedSmall:SetFont(NUMBER_TEXT_FONT, 11)

   GameFontNormalLarge:SetFont(NORMAL_TEXT_FONT, 17)
   GameFontHighlightLarge:SetFont(NORMAL_TEXT_FONT, 17)
   GameFontDisableLarge:SetFont(NORMAL_TEXT_FONT, 17)
   GameFontDisableLarge:SetTextColor(0.6, 0.6, 0.6)
   GameFontGreenLarge:SetFont(NORMAL_TEXT_FONT, 17)
   GameFontRedLarge:SetFont(NORMAL_TEXT_FONT, 17)

   GameFontNormalHuge:SetFont(BOLD_TEXT_FONT, 20)

   NumberFont_Shadow_Small:SetFont(NUMBER_TEXT_FONT, 13, 'OUTLINE')
   NumberFont_OutlineThick_Mono_Small:SetFont(NUMBER_TEXT_FONT, 13, 'OUTLINE')
   NumberFont_Shadow_Med:SetFont(NUMBER_TEXT_FONT, 15, 'OUTLINE')
   NumberFont_Outline_Med:SetFont(NUMBER_TEXT_FONT, 15, 'OUTLINE')
   NumberFont_Outline_Large:SetFont(NUMBER_TEXT_FONT, 17, 'OUTLINE')
   NumberFont_Outline_Huge:SetFont(NUMBER_TEXT_FONT, 30, 'OUTLINE')
   NumberFont_Outline_Huge:SetAlpha(30)

	ChatFontNormal:SetFont(chatFont, 14)
	ChatFontNormal:SetShadowColor(0.0, 0.0, 0.0)
	ChatFontNormal:SetShadowOffset(1, -1)

	ChatFontSmall:SetFont(chatFont, 13)
	ChatFontSmall:SetShadowColor(0.0, 0.0, 0.0)
	ChatFontSmall:SetShadowOffset(1, -1)
   
	-- CombatTextFont:SetFont(abf, 24)
   CombatLogFont:SetFont(chatFont, 12)

   -- for i=1, 7 do
      -- local f = _G['ChatFrame'..i]
      -- if f then
         -- f:SetFont(chatFont, 13)
      -- end
   -- end
   
   QuestFont_Large:SetFont(BOLD_TEXT_FONT, 19)
   QuestFont_Large:SetShadowColor(0.54, 0.4, 0.1)

   QuestFont:SetFont(chatFont, 14)
   QuestFont:SetTextColor(0.15, 0.09, 0.04)

   QuestFontNormalSmall:SetFont(chatFont, 13)
   QuestFontNormalSmall:SetShadowColor(0.54, 0.4, 0.1)

   QuestFontHighlight:SetFont(chatFont, 15)

   DialogButtonNormalText:SetFont(BOLD_TEXT_FONT, 17)
   DialogButtonHighlightText:SetFont(BOLD_TEXT_FONT, 17)

   InvoiceFont_Med:SetFont(chatFont, 13)
   InvoiceFont_Med:SetTextColor(0.15, 0.09, 0.04)

   InvoiceFont_Small:SetFont(chatFont, 11)
   InvoiceFont_Small:SetTextColor(0.15, 0.09, 0.04)

   ItemTextFontNormal:SetFont(NORMAL_TEXT_FONT, 16)

   MailFont_Large:SetFont(chatFont, 15)
   MailFont_Large:SetTextColor(0.15, 0.09, 0.04)
   MailFont_Large:SetShadowColor(0.54, 0.4, 0.1)
   MailFont_Large:SetShadowOffset(1, -1)

   PVPInfoTextFont:SetFont(NORMAL_TEXT_FONT, 22, 'THICKOUTLINE')

   SubSpellFont:SetFont(NORMAL_TEXT_FONT, 11)

   TextStatusBarText:SetFont(NUMBER_TEXT_FONT, 11, 'OUTLINE')

   if (CanSetFont(TextStatusBarTextSmall)) then
      TextStatusBarTextSmall:SetFont(NORMAL_TEXT_FONT, 13)
   end

   GameTooltipText:SetFont(NORMAL_TEXT_FONT, 13)
   GameTooltipTextSmall:SetFont(NORMAL_TEXT_FONT, 13)
   GameTooltipHeaderText:SetFont(BOLD_TEXT_FONT, 15, 'OUTLINE')

   WorldMapTextFont:SetFont(BOLD_TEXT_FONT, 31, 'THICKOUTLINE')
   WorldMapTextFont:SetShadowColor(0, 0, 0)
   WorldMapTextFont:SetShadowOffset(1, -1)
   WorldMapTextFont:SetAlpha(40)

   ZoneTextFont:SetFont(BOLD_TEXT_FONT, 31, 'THICKOUTLINE')
   ZoneTextFont:SetShadowColor(0, 0, 0)
   ZoneTextFont:SetShadowOffset(1, -1)

   SubZoneTextFont:SetFont(BOLD_TEXT_FONT, 27, 'THICKOUTLINE')

   BossEmoteNormalHuge:SetFont(NORMAL_TEXT_FONT, 25)

end

AddEventListener('PLAYER_LOGIN', Spy.ApplyFonts)